/*******************************************************************************
 * @brief: systask
 * @Author: aple
 * @Date: 2022-12-04 10:37:03
 * @LastEditors: aple
 * @LastEditTime: 2022-12-07 19:27:41
 * @Verision: 1.0.0.0
 ******************************************************************************/
#include "mbedtls/aes.h" 
#include "bsp_api.h"
#include "sys_task.h"
#include "pincfg.h"
#include "mytype.h"
#include "sys_init.h"
#include "framework.h"


/*****************************************************************************
 * @brief   var
 *****************************************************************************/
extern CRYP_HandleTypeDef hcryp;

// 此数组为 key_code_sim 代码二进制值
// 作为固定键值，可以伪装为程序
const uint16_t ase_key_src[] = {
	0xf04f, 0x5000, 0x6801, 0x07ca,
	0xd500, 0xbf00, 0x6841, 0x078a,
	0xd500, 0xbf00, 0x6881, 0x074a,
	0xd500, 0xbf00, 0x68c0, 0x0701,
	0xd500, 0xbf00, 0x4770};

#define dat_buff_init()              \
	loop(1000) src[index] = index;   \
	memset(enc, 0, 1000);            \
	memset(dec, 0, 1000);


/*****************************************************************************
 * @brief   fun
 *****************************************************************************/

/*******************************************************************************
* @brief  des_demo.
* \param[in] none
* \retval: none
*******************************************************************************/
int aes_demo(int argc, char * argv[])
{
	uu32 clk_start;
	uu8 *src, *enc, *dec, *iv;
	mbedtls_aes_context *aes_enc, *aes_dec;
	size_t offset;

	// 内存请求
	src = calloc(1, 1024);
	enc = calloc(1, 1024);
	dec = calloc(1, 1024);
	iv = calloc(1, 80);
	aes_enc = malloc(sizeof(mbedtls_aes_context));
	aes_dec = malloc(sizeof(mbedtls_aes_context));

	dbg_puts("\n===========================================================\n");
	dbg_print("AES 128 encode test: <128 bits> \n");

	// 初始化
	dat_buff_init();

	// 初始化结构
	mbedtls_aes_init(aes_enc);
	mbedtls_aes_init(aes_dec);

	// 生成 KEY
	clk_start = bsp_cpu_clks_get();
	mbedtls_aes_setkey_enc(aes_enc, (void *)ase_key_src, 128);
	mbedtls_aes_setkey_dec(aes_dec, (void *)ase_key_src, 128);
	dbg_print("key make time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));

	/*------------------------------------------------------------------------*/
	// 加密 + 解密 	ECB			电子密码本模式
	clk_start = bsp_cpu_clks_get();
	loop(32)
	{
		mbedtls_aes_crypt_ecb(aes_enc, MBEDTLS_AES_ENCRYPT, &src[16*index], &enc[16*index]);
		mbedtls_aes_crypt_ecb(aes_dec, MBEDTLS_AES_DECRYPT, &enc[16*index], &dec[16*index]);
	}
	dbg_print("MBEDTLS: AES ecb : enc + dec time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));

	// 显示
	//dbg_u8_print("src: ", src, 16, 20);
	dbg_u8_print("enc: ", enc, 128, 16);
	dbg_u8_print("dec: ", dec, 128, 16);

	// 释放，用于清除内存中的密匙，属于安全性代码
	mbedtls_aes_free(aes_enc);
	mbedtls_aes_free(aes_dec);

	// 硬件加密解密
	dat_buff_init();
	clk_start = bsp_cpu_clks_get();
	HAL_CRYP_Encrypt(&hcryp, (uu32*)&src[0], 512/4, (uu32*)&enc[0], 100000);
	HAL_CRYP_Decrypt(&hcryp, (uu32*)&enc[0], 512/4, (uu32*)&dec[0], 100000);	
	dbg_print("STM32: AES ecb : enc + dec time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));
	dbg_u8_print("enc: ", enc, 128, 16);
	dbg_u8_print("dec: ", dec, 128, 16);	
	
	// 释放内存
	free(src);
	free(enc);
	free(dec);
	free(iv);
	free(aes_enc);
	free(aes_dec);
	
	return 0;
}
DBG_FUN_EXPORT(aes_demo);

